package glen.mgbean;

import glen.security.dao.GroupDAO;
import glen.security.dto.Group;
import glen.security.dto.User;
import glen.security.service.UserSvr;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.context.RequestContext;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class GroupMgb implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -7426842830317461119L;
    /**
     *
     */

    private Integer groupIdSearch;
    private String groupNameSearch;
    private Integer activeSearch;
    private List<Group> resultSearch;
    private List<User> userGroup;
    private Group selected;
    private DataTable dtResult;
    private Integer userIdAdded;
    private List<User> userList;
    private GroupDAO dao = new GroupDAO();
    private Boolean isUpdateUserGroup = false;

    public Integer getUserIdAdded() {
        return userIdAdded;
    }

    public void setUserIdAdded(Integer userAdded) {
        this.userIdAdded = userAdded;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }


    public DataTable getDtResult() {
        return dtResult;
    }

    public void setDtResult(DataTable dtResult) {
        this.dtResult = dtResult;
    }

    @Override
    public String toString() {
        return "GroupMgb [groupIdSearch=" + groupIdSearch + ", groupNameSearch="
                + groupNameSearch + ", activeSearch=" + activeSearch
                + ", resultSearch=" + resultSearch + ", selected=" + selected
                + "]";
    }

    public Integer getGroupIdSearch() {
        return groupIdSearch;
    }

    public void setGroupIdSearch(Integer groupIdSearch) {
        this.groupIdSearch = groupIdSearch;
    }

    public String getGroupNameSearch() {
        return groupNameSearch;
    }

    public void setGroupNameSearch(String groupNameSearch) {
        this.groupNameSearch = groupNameSearch;
    }

    public Integer getActiveSearch() {
        return activeSearch;
    }

    public void setActiveSearch(Integer activeSearch) {
        this.activeSearch = activeSearch;
    }

    public List<Group> getResultSearch() {
        return resultSearch;
    }

    public void setResultSearch(List<Group> resultSearch) {
        this.resultSearch = resultSearch;
    }

    public Group getSelected() {
        return selected;
    }

    public void setSelected(Group selected) {
        this.selected = selected;
    }

    public void add() {
        selected = new Group();
        userGroup = new ArrayList<User>();
    }

    public void reset() {
        groupIdSearch = null;
        groupNameSearch = null;
        selected = null;
    }

    public void search() {

        Group o = new Group();
        o.setGroupId(groupIdSearch);
        o.setGroupName(groupNameSearch);
        resultSearch = dao.search(o);
    }

    public void view(Group item) {
        selected = item;
        userGroup = dao.searchUserGroup(item.getGroupId());
        userList = dao.listUser(item.getGroupId());
    }

    public List<User> getUserGroup() {
        return userGroup;
    }

    public void setUserGroup(List<User> userGroup) {
        this.userGroup = userGroup;
    }

    public boolean checkDup(String value) {
        Group item = new Group();
        item.setGroupName(value);
        if (dao.search(item).size() == 0)
            return false;
        return true;
    }

    public void save() {
        FacesContext context = FacesContext.getCurrentInstance();

        if (selected.getGroupId() != null)
            dao.update(selected);
        else {
            if (checkDup(selected.getGroupName())) {
                RequestContext.getCurrentInstance().addCallbackParam(
                        "checkBusinessFail", true);
                context.addMessage("growl", new FacesMessage(
                        FacesMessage.SEVERITY_ERROR, "Add Error",
                        "Duplicate Name:" + selected.getGroupName()));
                return;
            }
            dao.create(selected);
        }
        if (isUpdateUserGroup) {
            dao.saveUserGroup(selected.getGroupId(), userGroup);
        }
        context.addMessage("growl", new FacesMessage("Operation Success",
                "Save ID: " + selected.getGroupId()));
        search();

    }

    public void delete() {

        dao.delete(selected.getGroupId());
        search();
        FacesContext.getCurrentInstance().addMessage(
                "growl",
                new FacesMessage("Operation Success", "Delete ID: "
                        + selected.getGroupId())
        );
    }

    public void addUserGroup() {
        isUpdateUserGroup = true;
        UserSvr udao = new UserSvr();
        User user = udao.get(userIdAdded);
        if (user != null) {
            for (User u : userList) {
                if (u.getUserId() == user.getUserId()) {
                    userList.remove(u);
                    break;
                }
            }

            userGroup.add(user);
        }
        Collections.sort(userList, new Comparator<User>() {
            public int compare(User o1, User o2) {
                return o1.getUserId().compareTo(o2.getUserId());
            }
        });
    }

    public void removeUserGroup(User user) {
        isUpdateUserGroup = true;
        for (User u : userGroup) {
            if (u.getUserId() == user.getUserId()) {
                userGroup.remove(u);
                userList.add(u);
                break;
            }
        }
        Collections.sort(userList, new Comparator<User>() {
            public int compare(User o1, User o2) {
                return o1.getUserId().compareTo(o2.getUserId());
            }
        });
    }
}
